# -*- coding:utf-8 -*-
import qdarkstyle
from qdarkstyle import LightPalette, DarkPalette

# -*- coding:utf-8 -*-

from common.import_pyside import *
from common.data import DB
from utils.read_ini import read_ini


class ConfigSettingWindow(QDialog):
    def __init__(self, parent=None):
        super(ConfigSettingWindow, self).__init__(parent=parent)
        self.setWindowIcon(QIcon('./icon/system.ico'))
        self.resize(600, 80)
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setWindowTitle("配置设置")
        self.setTheme()

        gLayout = QGridLayout()
        cancelButton = QPushButton("取消")
        saveButton = QPushButton("保存")

        gLayout.addWidget(self.themeSetting(), 0, 0)
        gLayout.addWidget(self.remoteSetting(), 0, 1)
        gLayout.addWidget(self.browserSetting(), 1, 1)
        gLayout.addWidget(self.databaseSetting(), 1, 0)

        gLayout.addWidget(cancelButton, 2, 0)
        gLayout.addWidget(saveButton, 2, 1)

        self.setLayout(gLayout)

    def themeSetting(self):
        groupBox = QGroupBox('主题设置', self)
        formLayout = QFormLayout(self)
        formLayout.setSpacing(10)
        formLayout.setLabelAlignment(Qt.AlignRight)

        self.theme = QComboBox(self)
        self.theme.addItems(['light', 'dark'])
        self.theme.setCurrentIndex(0)
        self.theme.currentIndexChanged.connect(self.databaseChanged)
        formLayout.addRow(QLabel('选择主题:', self), self.theme)

        groupBox.setLayout(formLayout)
        return groupBox

    def browserSetting(self):
        groupBox = QGroupBox('浏览器设置', self)

        formLayout = QFormLayout(self)
        formLayout.setSpacing(10)
        formLayout.setLabelAlignment(Qt.AlignRight)
        self.is_open_browser = QComboBox(self)
        self.is_open_browser.addItems(['否', '是'])
        self.is_open_browser.currentIndexChanged.connect(self.databaseChanged)

        self.browser = QComboBox(self)
        self.browser.addItems(['fireFox', 'edge'])
        self.browser.currentIndexChanged.connect(self.databaseChanged)

        self.get_webdriver = QPushButton(self)
        self.get_webdriver.setText("选择webdriver")

        formLayout.addRow(QLabel('是否打开浏览器:', self), self.is_open_browser)
        formLayout.addRow(QLabel('选择浏览器:', self), self.browser)

        formLayout.addRow(QLabel('选择webdriver:', self), self.get_webdriver)
        groupBox.setLayout(formLayout)
        return groupBox

    def databaseSetting(self):
        groupBox = QGroupBox('数据库设置', self)

        formLayout = QFormLayout(self)
        formLayout.setSpacing(10)
        formLayout.setLabelAlignment(Qt.AlignRight)

        self.database = QComboBox(self)
        self.database.addItems(['sqlite', 'mysql'])
        self.database.currentIndexChanged.connect(self.databaseChanged)

        self.sqlite_name = QLineEdit(self)
        self.sqlite_name.setPlaceholderText("请输入sqlite数据库名")

        self.mysql_name = QLineEdit(self)
        self.mysql_name.setPlaceholderText("请输入mysql数据库名")

        self.mysql_host = QLineEdit(self)
        self.mysql_host.setPlaceholderText("请输入mysql数据库地址")

        self.mysql_port = QLineEdit(self)
        self.mysql_port.setPlaceholderText("请输入mysql数据库端口")

        self.mysql_user = QLineEdit(self)
        self.mysql_user.setPlaceholderText("请输入mysql数据库用户名")

        self.mysql_password = QLineEdit(self)
        self.mysql_password.setPlaceholderText("请输入mysql数据库密码")
        self.mysql_password.setEchoMode(QLineEdit.Password)

        self.connectTest = QPushButton("连接测试")

        formLayout.addRow(QLabel('选择数据库:', self), self.database)
        formLayout.addRow(QLabel('sqlite数据库名:', self), self.sqlite_name)
        formLayout.addRow(QLabel('mysql数据库名:', self), self.mysql_name)
        formLayout.addRow(QLabel('mysql数据库地址:', self), self.mysql_host)
        formLayout.addRow(QLabel('mysql数据库端口:', self), self.mysql_port)
        formLayout.addRow(QLabel('mysql数据库用户名:', self), self.mysql_user)
        formLayout.addRow(QLabel('mysql数据库密码:', self), self.mysql_password)
        formLayout.addRow(self.connectTest)

        groupBox.setLayout(formLayout)
        return groupBox

    def remoteSetting(self):
        groupBox = QGroupBox('远程设置', self)
        formLayout = QFormLayout(self)
        formLayout.setSpacing(10)
        formLayout.setLabelAlignment(Qt.AlignRight)

        self.remote_host = QLineEdit(self)
        self.remote_host.setPlaceholderText("请输入远程地址")

        formLayout.addRow(QLabel('远程地址:', self), self.remote_host)

        groupBox.setLayout(formLayout)
        return groupBox

    def databaseChanged(self):
        database = self.database.currentText()
        print(database)
        pass

    def showEvent(self, event) -> None:
        print("show")

    def hideEvent(self, event) -> None:
        print("hide")

    def setTheme(self):
        if read_ini("theme", "palette") == "light":
            self.setStyleSheet(qdarkstyle.load_stylesheet(qt_api="pyside2",
                                                          palette=LightPalette) + 'QGroupBox::title{margin-top: 5px;}')
        if read_ini("theme", "palette") == "dark":
            self.setStyleSheet(qdarkstyle.load_stylesheet(qt_api="pyside2",
                                                          palette=DarkPalette) + 'QGroupBox::title{margin-top: 5px;}')


if __name__ == '__main__':
    app = QApplication()
    win = ConfigSettingWindow()
    win.show()
    app.exec_()
